package klass.parse.domain;

public class AccessFlag {

    private short value;

    public static short ACC_PUBLIC = 0X0001;
    public static short ACC_FINAL = 0X0010;
    public static short ACC_SUPER = 0X0020;
    public static short ACC_INTERFACE = 0X0200;
    public static short ACC_ABSTRACT = 0X0400;
    public static short ACC_SYNTHETIC = 0X1000;
    public static short ACC_ANNOTATION = 0X2000;
    public static short ACC_ENUM = 0X4000;

    public AccessFlag(short value) {
        this.value = value;
    }

    public boolean isPublic() {
        return is(ACC_PUBLIC);
    }

    public boolean isFinal() {
        return is(ACC_FINAL);
    }

    public boolean isSuper() {
        return is(ACC_SUPER);
    }

    public boolean isInterface() {
        return is(ACC_INTERFACE);
    }

    public boolean isAbstract() {
        return is(ACC_ABSTRACT);
    }

    public boolean isSynthetic() {
        return is(ACC_SYNTHETIC);
    }

    public boolean isAnnotation() {
        return is(ACC_ANNOTATION);
    }

    public boolean isEnum() {
        return is(ACC_ENUM);
    }

    public boolean is(short flag) {
        return flag == (flag & value);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        if (isPublic())
            builder.append(" ACC_PUBLIC,");
        if (isFinal())
            builder.append(" ACC_FINAL,");
        if (isSuper())
            builder.append(" ACC_SUPER,");
        if (isInterface())
            builder.append(" ACC_INTERFACE,");
        if (isAbstract())
            builder.append(" ACC_ABSTRACT,");
        if (isSynthetic())
            builder.append(" ACC_SYNTHETIC,");
        if (isAnnotation())
            builder.append(" ACC_ANNOTATION,");
        if (isEnum())
            builder.append(" ACC_ENUM,");
        if (builder.length() > 0)
            builder.deleteCharAt(builder.lastIndexOf(","));
        return builder.toString();
    }
}
